from rest_framework.authentication import BaseAuthentication, SessionAuthentication
from rest_framework import exceptions
from django.shortcuts import redirect
from apps.users.models import UserInfo


# class MyTokenAuthentication(BaseAuthentication):
#     def authenticate(self, request):
#         token = request.query_params["token"]
#         token_obj = Token.objects.filter(user_token=token).first()
#         if not token_obj:
#             raise exceptions.AuthenticationFailed("用户认证失败")
#         return token_obj.user, token_obj

class MySessionAuthentication(BaseAuthentication):
    def authenticate(self, request):
        user_data = request.session.get("user", None)
        if user_data is None:
            raise exceptions.AuthenticationFailed("用户认证失败")
        user_obj = UserInfo.objects.filter(**user_data).first()
        return user_obj, None
